package com.shangxia.spr.init;

import com.shangxia.spr.Service.AttributeService;
import com.shangxia.spr.enums.AttributeCategoryEnum;
import com.shangxia.spr.enums.npc.BaseNpcEnum;
import com.shangxia.spr.frame.AttributeI;
import com.shangxia.spr.mapper.dao.Attribute;
import com.shangxia.spr.po.vo.WarTimeAttributeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zhouya38158
 * @date 2021/11/19 10:30
 * @apiNote 初始化 角色
 */
@Slf4j
@Component
public class InitNpc implements CommandLineRunner {

    @Autowired
    private AttributeService attributeService;

    @Override
    public void run(String... args) throws Exception {
        log.info("项目启动 初始化数据任务 开始");
        log.info("--------------------");
        log.info("--------------------");
        log.info("--------------------");
        log.info("--------------------");
        log.info("项目启动 初始化数据任务 结束");
    }

    public void initZuoYou() {
        WarTimeAttributeVO warTimeAttributeVO = BaseNpcEnum.ZUO_YOU.getWarTimeAttributeVO();
        List<Attribute> attributes = attributeService.listAllByColumn("npc_code", BaseNpcEnum.ZUO_YOU.getNpcCode());
        fillAttribute(attributes, warTimeAttributeVO);
    }

    public void fillAttribute(List<Attribute> attributes, AttributeI attributeI) {
        updateAttribute(attributes, attributeI, null);
    }

    public void updateAttribute(List<Attribute> attributes, AttributeI attributeI, AttributeCategoryEnum categoryEnum) {
        if (null != attributeI.getAttributeImpl()) {
            fillAttribute(attributes, attributeI.getAttributeImpl());
        }
        boolean flag = true;
        for (AttributeCategoryEnum value : AttributeCategoryEnum.values()) {
            if (null != categoryEnum) {
                if ((flag && !value.equals(categoryEnum))) {
                    continue;
                }
                flag = false;
            }
            log.info("属性大类 AttributeCategoryEnum={}", value);
            attributes.stream().filter(e -> value.equals(e.getAttributeCategory()))
                    .peek(e -> attributeI.putField(e, e.getAttributeType())).close();
        }

    }

}
